//
//  LCYHTTPRequestOperationManager.m
//  DFace
//
//  Created by lcy on 14-8-22.
//  Copyright (c) 2014年 lcy. All rights reserved.
//

#import "LCYHTTPRequestOperationManager.h"

#import "UIDevice+Hardware.h"

static NSString *agent;
static NSString *get_agent(){
    if(agent == NULL){
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
        NSString *app_type = [infoDictionary objectForKey:@"CFBundleIdentifier"];
        NSString *deviceModel = [[UIDevice currentDevice] platformString];
        NSString *osVersion = [NSString stringWithFormat:@"iOS%@",[[UIDevice currentDevice] systemVersion]];
        NSString *type = @"";
        if([@"zjy.dface.cn" isEqualToString:app_type]){
            type = @"zjy";
        } else if ([@"zxwz.dface.cn" isEqualToString:app_type]) {
            type = @"zxwz";
        }
        
        agent = [[NSString alloc]initWithFormat:@"i%@%@-%@-%@", type,app_build,deviceModel,osVersion];
    }
    return agent;
}

@implementation LCYHTTPRequestOperationManager

- (AFHTTPRequestOperation *)GET:(NSString *)URLString
                     parameters:(id)parameters
                        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
    
    [request setAllHTTPHeaderFields:[self getCookies]];
    [request setValue:get_agent() forHTTPHeaderField:@"User-Agent"];
    
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    
    operation.securityPolicy = [self getSecurityPolicy];
    
    [self.operationQueue addOperation:operation];
    
    return operation;
}

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(id)parameters
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
    
    [request setAllHTTPHeaderFields:[self getCookies]];
    [request setValue:get_agent() forHTTPHeaderField:@"User-Agent"];

    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    
    operation.securityPolicy = [self getSecurityPolicy];

    [self.operationQueue addOperation:operation];
    
    return operation;
}

#pragma mark - Cookies

- (NSDictionary *)getCookies
{
    NSString * saveSessionId = @"30d7f9d0cef323c9dceeeff7902797e5";////nUserSettings.sessionId
    NSDictionary *properties = [[NSMutableDictionary alloc] init];
    [properties setValue:saveSessionId forKey:NSHTTPCookieValue];
    [properties setValue:@"_session_id" forKey:NSHTTPCookieName];
    [properties setValue:@"www.test.cn" forKey:NSHTTPCookieDomain];
    [properties setValue:@"/" forKey:NSHTTPCookiePath];
    
    //NSHTTPCookie *httpCookie = [[NSHTTPCookie alloc] initWithProperties:properties];
    //NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:httpCookie];
    
    return properties;
}

#pragma mark - Cookies

- (AFSecurityPolicy *)getSecurityPolicy
{
    AFSecurityPolicy *sec=[[AFSecurityPolicy alloc] init];
    [sec setAllowInvalidCertificates:YES];
    return sec;
}

@end
